import Router from 'vue-router';

import routeIndex1 from './index-1.js';
import routeIndex2 from './index-2.js';
import routeIndex3 from './index-3.js';

const Routes = routeIndex1.concat(routeIndex2, routeIndex3);

export default () => {
    const siteName = '';

    // 页面的 title settings
    const initializeSiteTitle = (to, from) => {
        var siteName = siteName ? ' - ' + siteName : '',
            title = to.meta.title ? to.meta.title + siteName : '忘了设置页面标题啦 ~~~';

        window.document.title = title;
    };

    // mode: history | hash
    const router = new Router({
        mode: 'hash',
        routes: Routes
    });

    router.beforeEach((to, from, next) => {
        var user = window.sessionStorage.getItem('user') || false,
            checkAdoptPath = function(path) {
                var pathArray = [
                        '/login', '/shop/regedit', '/public/forgotPassWord','/public/map'
                    ],
                    adopt = false;

                for(let x of pathArray) {
                    if(path == x) {
                        adopt = true;
                        break;
                    }
                }

                return adopt;
            };

        if(user || checkAdoptPath(to.path)) {
            // 设置页面标题
            initializeSiteTitle(to, from);
            // console.log(to.path);
            window.sessionStorage.setItem('RoutePath', to.path);
            // 正常路由到下一步
            next();
        } else {
            next('/login');
        }
    });

    router.afterEach((to, from, next) => {
        // 定位滚动条
        window.scrollTo(0, 0);
    });

    return router;
};
